package eraikikon;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import java.awt.FlowLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.awt.Dimension;
import java.awt.event.ItemEvent;

public class SarbideakBistaratu extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel1;

	private JTextField jTextField;

	private JComboBox jComboBox;

	private JButton jButton;

	private ResultSetTableModel tableModel;
	
	private Box box1;

	private Box box2;

	private Box box3;

	private Box box4;

	private Box box5;

	private Box box6;

	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

	static final String DATABASE_URL = "jdbc:mysql://localhost/eraikikon2";

	Connection connection = null;

	Statement statement = null;

	/**
	 * This is the default constructor
	 */
	public SarbideakBistaratu() {
		super();
		initialize();
		try {
			tableModel = new ResultSetTableModel("com.mysql.jdbc.Driver",
					"jdbc:mysql://localhost/eraikikon2", "ikasle01",
					"ikasle01", "SELECT * FROM sarbideeskaera");
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		box1 = Box.createHorizontalBox();
		box2 = Box.createVerticalBox();
		box3 = Box.createHorizontalBox();
		box4 = Box.createHorizontalBox();
		box5 = Box.createVerticalBox();
		box6 = Box.createVerticalBox();
		jLabel1 = new JLabel("Sar ezazu nahi duzun data:           ");
		jTextField = new JTextField("Urtea-Hilabetea-Eguna");
		jTextField.setInputVerifier(new KonprobatuOsokoa());
		jTextField.setForeground(Color.blue);

		jComboBox = new JComboBox();
		jComboBox.addItem("            ");
		jButton = new JButton("Ateak erakutsi");
		box1.add(jLabel1);
		box1.add(jButton);
		box2.add(box1);
		box2.add(jTextField);
		box4.add(jComboBox);
		box6.add(box4);
		JLabel kk = new JLabel();
		kk.setText("                 ");
		box5.add(kk);
		// box3.add(jComboBox);
		box6.add(box5);
		box3.add(box2);
		box3.add(box6);
		jContentPane.add(box3, BorderLayout.NORTH);
		JTable resultTable = new JTable(tableModel);
		JScrollPane jScrollPane1 = new JScrollPane(resultTable,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jContentPane.add(jScrollPane1, BorderLayout.CENTER);
		jButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				if((jTextField.getText().compareTo("Urtea-Hilabetea-Eguna")==0)||(jTextField.getText().compareTo("")==0))
						JOptionPane.showMessageDialog(null, "Testu kutxan data sartu mesedez!");
				else{
				try {
					DBKud db = new DBKud();
					String data1 = jTextField.getText();
					String[] tarteko = data1.split("-");
					int zenb = Integer.parseInt(tarteko[2]);
					zenb++;
					String data2 = new String(tarteko[0] + "-" + tarteko[1]
							+ "-" + zenb);
					ResultSet resultSet = db.execSQL21(data1, data2);
					jComboBox.addItem("solairua - atea");
					while (resultSet.next()) {
						Integer emaitza1 = resultSet.getInt("solairua");
						Integer emaitza2 = resultSet.getInt("ateaID");
						jComboBox.addItem(emaitza1 + "-" + emaitza2);

					}
				} catch (SQLException sqlException) {
					sqlException.printStackTrace();
					System.exit(1);

				}
				}
			}
		});
		jComboBox.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent e) {
				// TODO Auto-generated Event stub itemStateChanged()
				if (e.getStateChange() == ItemEvent.SELECTED) {
					String elem = (String) e.getItem();
					String[] zatiak = elem.split("-");
					int solairua = Integer.parseInt(zatiak[0]);
					int atea = Integer.parseInt(zatiak[1]);
					String data1 = jTextField.getText();
					String[] tarteko = data1.split("-");
					int zenb = Integer.parseInt(tarteko[2]);
					zenb++;
					String data2 = new String(tarteko[0] + "-" + tarteko[1]
							+ "-" + zenb);
					try {

//						ResultSet resultSet = statement.executeQuery("SELECT data,txartelID,guneaID AS jatorriGuneaID, helburuGuneaID,baimenduta, ukapenarenArrazoia FROM sarbideeskaera NATURAL JOIN txartelirakurgailua WHERE solairua=" + solairua + " AND ateaID=" + atea);
						tableModel.setQuery("SELECT data,txartelID,guneaID AS jatorriGuneaID, helburuGuneaID,baimenduta, ukapenarenArrazoia FROM sarbideeskaera NATURAL JOIN txartelirakurgailua NATURAL JOIN atea WHERE data > '"
			+ data1
			+ "' AND data < '"
			+ data2
			+ "' AND solairua=" + solairua + " AND ateaID=" + atea);
//						JScrollPane jScrollPane1 = new JScrollPane(resultTable1,	
//								ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
//								ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//						jContentPane.add(jScrollPane1, BorderLayout.CENTER);
						
					} catch (SQLException a) {
						a.printStackTrace();
					}
					
				}
			}

		});
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(647, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("Sarbideen kontsulta");
		this.setLocation(400, 300);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	public static void main(String[] args) {
		SarbideakBistaratu sarbideakBistaratu = new SarbideakBistaratu();
		sarbideakBistaratu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		sarbideakBistaratu.setVisible(true);
	}
}  //  @jve:decl-index=0:visual-constraint="124,14"
